package com.appspot.moviesessions;

import android.content.Context;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;

import com.appspot.moviesessions.provider.MovieSession;

public class LoaderFactoryImpl implements LoaderFactory {

	private CursorAdapterFactory cursorAdapterFactory;
	private ViewBinderFactory viewBinderFactory;
	private Context context;
	private CursorLoaderFactory cursorLoaderFactory;
	private UriParser uriParser;

	public LoaderFactoryImpl(Context context,
			CursorAdapterFactory cursorAdapterFactory,
			ViewBinderFactory viewBinderFactory,
			CursorLoaderFactory cursorLoaderFactory, UriParser uriParser) {
		this.cursorAdapterFactory = cursorAdapterFactory;
		this.viewBinderFactory = viewBinderFactory;
		this.context = context;
		this.uriParser = uriParser;
		this.cursorLoaderFactory = cursorLoaderFactory;

	}

	@Override
	public Loader movieListLoader() {
		SimpleCursorAdapter cursorAdapter = cursorAdapterFactory.create(
				context, R.layout.movie, null,
				new String[] { MovieSession.Movie.POSTER_URI },
				new int[] { R.id.movie_poster },
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		ViewBinder movieListViewBinder = viewBinderFactory
				.movieListViewBinder();

		cursorAdapter.setViewBinder(movieListViewBinder);
		return new GenericLoader(context, cursorLoaderFactory, cursorAdapter,
				uriParser.parse(MovieSession.STR_MOVIE_DIR_URI), new String[] {
						MovieSession.Movie._ID, MovieSession.Movie.NAME,
						MovieSession.Movie.POSTER_URI }, null, null, null);
	}

	@Override
	public Loader movieInfoLoader(String dataUri) {
		SimpleCursorAdapter cursorAdapter = cursorAdapterFactory.create(
				context, R.layout.movie_info_view, null,
				new String[] { MovieSession.Movie.NAME },
				new int[] { R.id.movie_info_name },
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		ViewBinder movieInfoViewBinder = viewBinderFactory
				.movieInfoViewBinder();
		cursorAdapter.setViewBinder(movieInfoViewBinder);
		return new GenericLoader(context, cursorLoaderFactory, cursorAdapter,
				uriParser.parse(dataUri), new String[] {
						MovieSession.Movie._ID, MovieSession.Movie.NAME },
				null, null, null);
	}

	@Override
	public Loader movieSpinnerLoader() {
		SimpleCursorAdapter cursorAdapter = cursorAdapterFactory.create(
				context, R.layout.movie_spinner_item, null,
				new String[] { MovieSession.Movie.NAME },
				new int[] { R.id.movie_spinner_item },
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		ViewBinder movieInfoViewBinder = viewBinderFactory
				.movieSpinnerViewBinder();
		cursorAdapter.setViewBinder(movieInfoViewBinder);
		return new GenericLoader(context, cursorLoaderFactory, cursorAdapter,
				uriParser.parse(MovieSession.STR_MOVIE_DIR_URI), new String[] {
						MovieSession.Movie._ID, MovieSession.Movie.NAME },
				null, null, null);
	}

	@Override
	public Loader locationSpinnerLoader(long movieId) {
		SimpleCursorAdapter cursorAdapter = cursorAdapterFactory.create(
				context, R.layout.location_spinner_item, null,
				new String[] { MovieSession.Location.NAME },
				new int[] { R.id.location_spinner_item },
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		ViewBinder locationInfoViewBinder = viewBinderFactory
				.locationSpinnerViewBinder();
		cursorAdapter.setViewBinder(locationInfoViewBinder);
		return new GenericLoader(context, cursorLoaderFactory, cursorAdapter,
				uriParser.parse(MovieSession.STR_LOCATION_DIR_URI),
				new String[] { MovieSession.Location._ID,
						MovieSession.Location.NAME }, null,
				new String[] { String.valueOf(movieId) }, null);
	}
}
